<template>
  <!-- 弹出层的内容 -->
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="14"
          color="white"
          @click="$emit('colsePop', false)"
        />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道 {{ show ? "点击移除频道" : "点击删除频道" }}</span>
        <!-- 点了编辑，切换有无小叉子的图标状态 -->
        <span @click="show = !show">{{ show ? "完成" : "编辑" }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="(obj, index) in userChannelList"
          :key="obj.id"
          @click="deleteFn(obj, index)"
        >
          <div class="channel-item van-hairline--surround">
            {{ obj.name }}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-if="show && index != 0"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in weixuan"
          :key="obj.id"
          @click="addpindao(obj)"
        >
          <div class="channel-item van-hairline--surround">{{ obj.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { allChannelListAPI, updateChannelApi } from '@/api'

export default {
  props: {
    userChannelList: {
      type: Array // 用户已选频道
    }
  },
  data () {
    return {
      allChannelList: [],
      show: false
    }
  },
  async created () {
    const all = await allChannelListAPI()
    this.allChannelList = all.data.data.channels
  },
  computed: {
    weixuan () {
      return this.allChannelList.filter((obj) => {
        // 进行筛选，筛选出用户没有选择的数据
        const index = this.userChannelList.findIndex((item) => {
          return item.id === obj.id // 要获取用户已选择的数据对应总数据的下标，若有下标，代表用户选了
        })
        if (index !== -1) {
          return false // 不等于-1代表已选，所以return false 不选这个
        } else {
          return true // 这里得到的就是用户未选的数据，因为index没有匹配到
        }
      })
    }
  },
  methods: {
    // 点击添加频道
    async addpindao (obj) {
      if (this.show === true) {
        this.userChannelList.push(obj)
        // 再调用更新服务器的请求
        this.updateFn()
        // //   const arr = [...this.userChannelList]//浅拷贝不行，因为只拷贝第一层
        // const arr = [] // 深拷贝，避免原数组里的name也被删除
        // this.userChannelList.forEach((item) => {
        //   const newobj = { ...item }
        //   arr.push(newobj)
        // })
        // const index = arr.findIndex((item) => {
        //   return item.name === '推荐'
        // })
        // arr.splice(index, 1)
        // arr.forEach((item, index) => {
        //   delete item.name // 删除对象里的键值对
        //   item.seq = index + 1
        // })
        // console.log(arr)
        // //   发送更新数据的请求
        // const res = await updateChannelApi({ channels: arr })
        // console.log(res)
      }
    },
    // 点击删除已选或跳转页面
    deleteFn (obj, index) {
      if (this.show === true && obj.name !== '推荐') {
        this.userChannelList.splice(index, 1)
        // 再调用更新服务器的请求
        this.updateFn()
      } else {
        console.log(1)
        this.$emit('colsePop', false)
        this.$emit('tiaozhuan', obj)
      }
    },
    // 发送更新请求
    async updateFn () {
      //   const arr = [...this.userChannelList]
      const arr = [] // 深拷贝，避免原数组里的name也被删除
      this.userChannelList.forEach((item) => {
        const newobj = { ...item }
        arr.push(newobj)
      })
      const index = arr.findIndex((item) => {
        return item.name === '推荐'
      })
      arr.splice(index, 1)
      arr.forEach((item, index) => {
        delete item.name // 删除对象里的键值对
        item.seq = index + 1
      })
      console.log(arr)
      //   发送更新数据的请求
      const res = await updateChannelApi({ channels: arr })
      console.log(res)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
